package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.core.JsonResult;
import com.wuming.caiwu.model.QsgStore;
import com.wuming.caiwu.model.SysAppletSet;
import com.wuming.caiwu.service.QsgStoreService;
import com.wuming.caiwu.service.SysAppletSetService;
import com.wuming.caiwu.util.DateUtil;
import com.wuming.caiwu.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/store")
public class QsgStoreController extends BaseController {

    @Autowired
    private QsgStoreService storeService;

    @Autowired
    private SysAppletSetService sysAppletSetService;

    @RequestMapping(value = "/manageQsgStore", method = RequestMethod.GET)
    public String manageQsgStore() {
        return "/store/manageQsgStore";
    }


    @ResponseBody
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public JsonResult listAll(){
        try {
            List<QsgStore> stores = storeService.getAll();
            return new JsonResult<Object>(stores, "success", true);
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return new JsonResult<Object>(null, "error", false);
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "error", false);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String storeName,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            List<QsgStore> students = storeService.getQsgStoresByPage(storeName, pageIndex, pageSize);
            int total = storeService.getTotal(storeName);
            return CommonResult.createSuccessResult(students, total, "查询店铺信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询店铺信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询店铺信息失败.");
        }
    }

    @RequestMapping(value = "/editQsgStore/{id}", method = RequestMethod.GET)
    public String editStudent(Model model, @PathVariable Integer id) {
        logger.info("准备修改店铺信息ID:{}", id);
        QsgStore product = storeService.getQsgStoreById(id);
        product.setSimOuttimeStr(DateUtil.dateToString(product.getSimOuttime()));
        model.addAttribute("store", product);
        return "/store/editQsgStore";
    }


    @RequestMapping(value = "/editAppletSetInfo/{storeId}", method = RequestMethod.GET)
    public String editAppletSetInfo(Model model, @PathVariable Long storeId) {
        logger.info("准备修改店铺设置信息ID:{}", storeId);
        SysAppletSet appletSet = sysAppletSetService.getSysAppletSetById(storeId);
        model.addAttribute("appletset", appletSet);
        model.addAttribute("storeId",storeId);
        return "/store/editAppletSet";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(QsgStore product) {
        try {
            if (null != product.getId()) {
                String simOuttimeStr = product.getSimOuttimeStr();
                product.setSimOuttime(DateUtil.parse(simOuttimeStr));
                storeService.modifyQsgStore(product);
            }
        } catch (Exception e) {
            logger.error("保存店铺信息失败,失败信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存店铺基本信息失败", false);
        }
        return new JsonResult<Object>(product.getId(), "保存店铺基本信息成功.", true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Integer id) {
        logger.info("准备删除店铺信息ID:{}", id);
        try {
            storeService.removeQsgStoreById(id);
            return CommonResult.createSuccessResult("删除店铺信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除店铺信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除店铺信息失败.");
        }
    }


    @ResponseBody
    @RequestMapping(value = "/saveAppletSet", method = RequestMethod.POST)
    public JsonResult saveAppletSet(SysAppletSet appletset) {
        try {
            SysAppletSet appletSet = sysAppletSetService.getSysAppletSetById(appletset.getStoreId());
            if (null != appletSet) {
                sysAppletSetService.modifySysAppletSet(appletset);
            }else{
                sysAppletSetService.saveSysAppletSet(appletset);
            }
        } catch (Exception e) {
            logger.error("保存店铺信息失败,失败信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存店铺基本信息失败", false);
        }
        return new JsonResult<Object>(appletset.getStoreId(), "保存店铺基本信息成功.", true);
    }

}
